﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}

<script>
    var PStructId = "";
    $(function () {
        InitialPage();
        GetTree();
        GetGrid();
    });
    //初始化页面
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 141.5);
            }, 200);
            e.stopPropagation();
        });

        $("#time_horizon").ComboBox({
            data: [{ "key": 4, "value": "上月" }, { "key": 3, "value": "本月" }, { "key": 5, "value": "本年" }],
            id: "key",
            text: "value",
            description: null,
            click: function (item) {
                var value = item.attr('data-value');
                switch (value) {
                    case "3"://本月
                        $("#SelectedStartTime").text("@DateTime.Now.ToString("yyyy年MM月")");
                        $("#SelectedEndTime").text("@DateTime.Now.AddMonths(1).ToString("yyyy年MM月")");
                        $("#Month1").val("@DateTime.Now.Month");
                        $("#Month2").val("@DateTime.Now.AddMonths(1).Month");
                        break;
                    case "4"://上月
                        $("#SelectedStartTime").text("@DateTime.Now.AddMonths(-1).ToString("yyyy年MM月")");
                        $("#SelectedEndTime").text("@DateTime.Now.ToString("yyyy年MM月")");
                        $("#Month1").val("@DateTime.Now.AddMonths(-1).Month");
                        $("#Month2").val("@DateTime.Now.Month");
                        break;
                    case "5"://本年
                        $("#SelectedStartTime").text("@DateTime.Now.AddDays(-DateTime.Now.DayOfYear + 1).ToString("yyyy年MM月")");
                        $("#SelectedEndTime").text("@DateTime.Now.AddMonths(12 - DateTime.Now.Month + 1).AddDays(-DateTime.Now.Day).ToString("yyyy年MM月")");
                        $("#Month1").val("01");
                        $("#Month2").val("12");
                        break;
                    default:
                        break;
                }
                $('#btn_Search').trigger("click");
            }
        });
        $("#time_horizon").ComboBoxSetValue(3);

        $("#DateType").ComboBox({
            data: [{ "key": 1, "value": "应收年月" }, { "key": 2, "value": "权责年月" }],
            id: "key",
            text: "value",
            description: null,
        });
        $("#DateType").ComboBoxSetValue(1);
        $("#IsAudit").ComboBox({
            data: [{ "key": -1, "value": "所有" }, { "key": 0, "value": "未审核" }, { "key": 1, "value": "已审核" }],
            id: "key",
            text: "value",
            description: null,
        });
        $("#IsAudit").ComboBoxSetValue(-1);
        var obj = { "CalType": 2 };
        //$("#ItemId").ComboBox({
        //    url: "../../FinancialManage/FeeItems/GetItemsTreeList",
        //    param: { "queryJson": JSON.stringify(obj) },
        //    id: "id",
        //    text: "text",
        //    description: "所有",
        //});
        $(".input-wdatepicker").css("background", "#FFF");
    }
    //加载树
    function GetTree() {
        var item = {
            height: $(window).height() - 21.5,
            url: "../../FinancialManage/ReceivableFeeItems/GetTree?IsSubstep=true",
            onnodeclick: function (item) {
                if (item.id == "All") {
                    PStructId = "";
                }
                if (item.type == "PStruct") {
                    PStructId = item.id;
                }
                if (item.type == "Vendor") {
                    PStructId = "";
                }
                $('#btn_Search').trigger("click");
            }
        };
        $("#itemTree").treeview(item);
    }

    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var queryJson = {
            DateType: $("#DateType").attr("data-value"),
            StartTime: $("#Year1").val() + $("#Month1").val(),
            EndTime: $("#Year2").val() + $("#Month2").val(),
            PStructId: PStructId,
            CustomerName: $("#CustomerName").val(),
            ItemId: $("#ItemId").attr("data-value"),
            IsAudit: $("#IsAudit").attr("data-value")
        };
        var $gridTable = $('#gridTable');
        $gridTable.jqGrid({
            url: "../../FinancialManage/ReceivableFeeItems/GetPageList",
            datatype: "json",
            postData: { queryJson: JSON.stringify(queryJson) },
            height: $(window).height() - 141.5,
            autowidth: true,
            colModel: [
                { label: '主键', name: 'Id', hidden: true },
                { label: "费项名称", name: "ItemId", width: 100, align: "left", sortable: false },
                { label: "客户名称", name: "CustomerId", width: 100, align: "left", sortable: false },
                { label: "房屋地址", name: "PStructId", width: 60, align: "left", sortable: false },
                {
                    label: "应收年月", name: "ReceiveMonth", width: 100, align: "left", sortable: false,
                    formatter: function (cellvalue) {
                        return cellvalue.substring(0, 4) + "年" + cellvalue.substring(4, 6) + "月";
                    }
                },
                { label: "计费起始日", name: "BeginDate", width: 100, align: "left", sortable: false },
                { label: "计费终止日", name: "EndDate", width: 100, align: "center", sortable: false },
                { label: "应收金额", name: "ReceiveAmount", width: 60, align: "center", sortable: false },
                {
                    label: "已收金额", name: "ReceivedAmount", width: 100, align: "left", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return !!cellvalue ? cellvalue : 0;
                    }
                },
                {
                    label: "未收金额", name: "UncollectedAmount", width: 100, align: "left", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return rowObject.ReceiveAmount - rowObject.ReceivedAmount;
                    }
                },
                {
                    label: "是否审核", name: "IsAudit", width: 100, align: "left", sortable: false, hidden: true,
                    formatter: function (cellvalue) {
                        return cellvalue == 1 ? "已审核" : "未审核";
                    }
                },
                { label: "附加说明", name: "Description", width: 200, align: "left", sortable: false }
            ],
            viewrecords: true,
            rowNum: 30,
            rowList: [30, 50, 100, 500, 1000],
            pager: "#gridPager",
            sortname: 'CreateDate desc',
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            onSelectRow: function (rowid) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            SearchEvent();
        });
    }
    //查询表格函数
    function SearchEvent() {
        var queryJson = {
            DateType: $("#DateType").attr("data-value"),
            StartTime: $("#Year1").val() + $("#Month1").val(),
            EndTime: $("#Year2").val() + $("#Month2").val(),
            PStructId: PStructId,
            CustomerName: $("#CustomerName").val(),
            ItemId: $("#ItemId").attr("data-value"),
            IsAudit: $("#IsAudit").attr("data-value")
        };
        $('#gridTable').jqGrid('setGridParam', {
            url: "../../FinancialManage/ReceivableFeeItems/GetPageList",
            postData: { queryJson: JSON.stringify(queryJson) }
        }).trigger('reloadGrid');
    }

    //新增
    function btn_add() {
        dialogOpen({
            id: "Form",
            title: '新增应收费用',
            url: '/FinancialManage/ReceivableFeeItems/Form',
            width: "1100px",
            height: "800px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    }
    //编辑
    function btn_edit() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "Form",
                title: '编辑应收费用',
                url: '/FinancialManage/ReceivableFeeItems/Form?keyValue=' + keyValue,
                width: "1100px",
                height: "800px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
    }

    //删除
    function btn_delete() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            $.RemoveForm({
                url: '/FinancialManage/ReceivableFeeItems/RemoveForm',
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            dialogMsg('请选择需要删除的应收费用！', 0);
        }
    }

    //审核
    function btn_audit() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            var IsAudit = $("#gridTable").jqGridRowValue("IsAudit");
            if (IsAudit == "已审核") {
                dialogMsg('该数据已经通过审核，无需再次审核！', 0);
                return false;
            }
            $.RemoveForm({
                url: '/FinancialManage/ReceivableFeeItems/CheckAudit',
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            dialogMsg('请选择需要审核的应收费用！', 0);
        }
    }


</script>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <div class="west-Panel">
            <div id="itemTree"></div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="center-Panel">
            <div class="titlePanel">
                <div class="title-search">
                    <table>
                        <tr>
                            <td>筛选条件</td>
                            <td style="padding-left: 10px;">
                                <div class="ui-filter" style="width: 200px;">
                                    <div class="ui-filter-text">
                                        <strong id="SelectedStartTime">@DateTime.Now.ToString("yyyy年MM月")</strong> 至 <strong id="SelectedEndTime">@DateTime.Now.AddMonths(1).ToString("yyyy年MM月")</strong>
                                    </div>
                                    <div class="ui-filter-list" style="width: 400px;">
                                        <table class="form" id="filter-form">
                                            <tr>
                                                <th class="formTitle" style="width: 80px;"><div id="DateType" type="selectTree" class="ui-select" style="width: 80px;"></div></th>
                                                <td class="formValue">
                                                    <div style="width: 45%; display: inherit;">
                                                        <input id="Year1" type="text" class="form-control input-wdatepicker" style="width:42%;display:inherit;" onfocus="WdatePicker({ dateFmt: 'yyyy' })" value="@DateTime.Now.ToString("yyyy")" />年
                                                        <input id="Month1" type="text" class="form-control input-wdatepicker" style="width: 32%; display: inherit;" onfocus="WdatePicker({ dateFmt: 'MM' })" value="@DateTime.Now.ToString("MM")" />月
                                                    </div>
                                                    <div style="width: 10%; text-align: center; display: inherit; ">至</div>
                                                    <div style="width: 45%;display:inherit;">
                                                        <input id="Year2" type="text" class="form-control input-wdatepicker" style="width:42%;display:inherit;" onfocus="WdatePicker({ dateFmt: 'yyyy' })" value="@DateTime.Now.AddMonths(1).ToString("yyyy")" />年
                                                        <input id="Month2" type="text" class="form-control input-wdatepicker" style="width: 32%; display: inherit;" onfocus="WdatePicker({ dateFmt: 'MM' })" value="@DateTime.Now.AddMonths(1).ToString("MM")" />月
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle" style="text-align:right;">收费项目：</td>
                                                <td class="formValue">
                                                    <div id="ItemId" type="selectTree" class="ui-select"></div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle" style="text-align:right;">审核状态：</td>
                                                <td class="formValue">
                                                    <div id="IsAudit" type="selectTree" class="ui-select"></div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle" style="text-align:right;">客户查找：</td>
                                                <td class="formValue">
                                                    <input id="CustomerName" type="text" class="form-control">
                                                </td>
                                            </tr>
                                        </table>
                                        <div class="ui-filter-list-bottom">
                                            <a id="btn_Reset" class="btn btn-default">&nbsp;重&nbsp;&nbsp;置</a>
                                            <a id="btn_Search" class="btn btn-primary">&nbsp;查&nbsp;&nbsp;询</a>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td style="padding-left: 10px;">
                                <div id="time_horizon" type="selectTree" class="ui-select" style="width: 60px;"></div>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="toolbar">
                    <div class="btn-group">
                        <a id="lr-replace" class="btn btn-default" onclick="reload();"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                        <a id="lr-add" class="btn btn-default" onclick="btn_add()"><i class="fa fa-plus"></i>&nbsp;新增</a>
                        <a id="lr-edit" class="btn btn-default" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>
                        <a id="lr-delete" class="btn btn-default" onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                        <a id="lr-wrench" class="btn btn-default" onclick="btn_audit()"><i class="fa fa-wrench"></i>&nbsp;审核</a>
                        <div class="btn-group">
                            <a id="lr-more" class="btn btn-default" data-toggle="dropdown"><i class="fa fa-reorder"></i>&nbsp;更多操作<span class="caret"></span></a>
                        </div>
                    </div>
                    <script>$('.toolbar').authorizeButton()</script>
                </div>
            </div>
            <div class="gridPanel">
                <table id="gridTable"></table>
                <div id="gridPager"></div>
            </div>
        </div>
    </div>

</div>
